var fmtState = (s)=>s==1?"启用":"禁用";
$(document).ready(function(){
    initAdd();
    initUpdate();
    initGrant();
})
function initAdd(){
    $("#add").dialog({
        title:"添加角色",
        btnName:"添加",
        btnBind:function(){
            var param=   $("#add form").serialize();
            $.post("sys/role/add",param,'text')
                .done((msg)=>{
                    toastr.warning(eval(msg));
                    $("#add").dialog("close");
                    $("#roleTb").bootstrapTable("refresh");
                });
        }
    });
}
function initUpdate(){
    $("#update").dialog({
        title:"修改角色",
        btnName:"修改",
        btnBind:function(){
            var param=   $("#update form").serialize();
            $.post("sys/role/update",param,'text')
                .done((msg)=>{
                    toastr.warning(eval(msg));
                    $("#update").dialog("close");
                    $("#roleTb").bootstrapTable("refresh");
                });
        }
    });
}
function add(){
    $("#add form")[0].reset();
    $("#add").dialog("open");
}
function optFmt(value,row,index){
    var del="<a class='btn btn-danger del'><i class='glyphicon glyphicon-remove'></i> 删除</a>";
    var update="<a class='btn btn-success update'><i class='glyphicon glyphicon-remove'></i> 修改</a>";
    return del+"&emsp;"+update;
}
var optEvents= {
    "click .del": function (event, value, row, index) {
       swal({
           title:"确实要删除么？",
           text:"该操作不可逆，请谨慎操作!",
           type : "warning",
          // showConfirmButton:true,
           showCancelButton:true,
           confirmButtonText:"确定",
           cancelButtonText:"取消"
       }).then(function(){
           var id=row.id;
           $.get("sys/role/del","id="+id,"text")
               .done(function(msg){
                   toastr.warning(eval(msg));
                   $("#roleTb").bootstrapTable("refresh");
               });
       });
    },
    "click .update": function (event, value, row) {
        //$("#update form").setForm(row);
        $("#update form [name=name]").val(row.name);
        $("#update form [name=id]").val(row.id);
        if(row.state ==1){
            $("#update form [name=state]:eq(0)").prop("checked",true);
        }else{
            $("#update form [name=state]:eq(1)").prop("checked",true);
        }
        $("#update").dialog("open");
    }
}

/*授权对话框初始化
* btnBind:执行授权
* */
function initGrant(){
    $("#grant").dialog({
        title:"角色授权",
        btnBind:function(){
            var rows01=$("#roleTb").bootstrapTable("getSelections");
            var params="roleId="+rows01[0].id;
            var arr=$("#tree").treeview("getChecked");
            arr.forEach(node=>{
                params+="&menuIds="+node.id;
            });
            $.post("sys/permission/grant/",params)
                .done(msg=>{
                    $("#grant").dialog("close");
                    toastr.warning(eval(msg));
                    $("#userTb").bootstrapTable("refresh");
                });
        }
    });
}

/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
/*实现级联勾选结束*/

function grant() {
    var rows=$("#roleTb").bootstrapTable("getSelections");
    if(rows.length!=1){
        swal("请选择一项待授权！");
        return;
    }
    var roleId=rows[0].id;
    $.getJSON("sys/permission/search4Tree/"+roleId)
        .done(
            data =>{
                $("#tree").treeview({
                    multiSelect:true,
                    onNodeChecked:nodeChecked,
                    onNodeUnchecked:nodeUnchecked,
                    showCheckbox:true,
                    data
                });
                $("#grant").dialog("open");
            }
        );
}

